package com.forevershine.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.forevershine.business.context.ContextBuilder;
import com.forevershine.business.model.StyleDetail;
import com.forevershine.business.service.AbstractService;
import com.forevershine.business.service.impl.EmployeeServiceImpl;
import com.forevershine.personel.model.Employee;
import com.opensymphony.xwork2.ActionContext;

public class UpdateEmployeeAction extends BaseAction {

	private static Log log = LogFactory.getLog(UpdateEmployeeAction.class);

	private StyleDetail briefVo = new StyleDetail();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AbstractService service;

	private List employeeList = new ArrayList();

	public List getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List employeeList) {
		this.employeeList = employeeList;
	}

	private Employee employee;

	public StyleDetail getBriefVoModel() {
		return briefVo;
	}

	public void setBriefVoModel(StyleDetail model) {
		this.briefVo = model;
	}

	private void loadDefaultMenu() {

	}

	@Override
	protected String service() {
		return SUCCESS;
	}

	public void updateEmployee() {
		ActionContext context = ActionContext.getContext();
		Map params = ActionContext.getContext().getParameters();
		PrintWriter pw = null;
		try {
			pw = ((HttpServletResponse) context
					.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE))
					.getWriter();

			Employee employee = new Employee();
			if (!params.entrySet().isEmpty()) {
				employee.setEmployeeid(Integer.valueOf(((String[]) params
						.get("id"))[0].toString()));
				employee.setUsername(((String[]) params.get("username"))[0]
						.toString());
				employee.setPassword(((String[]) params.get("password"))[0]
						.toString());

			} else {
				pw.write("Error");

				return;
			}
			this.service = ContextBuilder.getContext().getInstance(
					EmployeeServiceImpl.class);
			((EmployeeServiceImpl) service).updateEmployee(employee);
			pw.write("Success");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	}
	
	public void addEmployee() {
		ActionContext context = ActionContext.getContext();
		Map params = ActionContext.getContext().getParameters();
		PrintWriter pw = null;
		try {
			pw = ((HttpServletResponse) context
					.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE))
					.getWriter();

			Employee employee = new Employee();
			if (!params.entrySet().isEmpty()) {
				employee.setUsername(((String[]) params.get("username"))[0]
						.toString());
				employee.setPassword(((String[]) params.get("password"))[0]
						.toString());

			} else {
				pw.write("Error");

				return;
			}
			this.service = ContextBuilder.getContext().getInstance(
					EmployeeServiceImpl.class);
			((EmployeeServiceImpl) service).addEmployee(employee);
			pw.write("Success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	
	}
	
	public void deleteEmployee()
	{
		ActionContext context = ActionContext.getContext();
		Map params = ActionContext.getContext().getParameters();
		PrintWriter pw = null;
		try {
			pw = ((HttpServletResponse) context
					.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE))
					.getWriter();

			Employee employee = new Employee();
			if (!params.entrySet().isEmpty()) {
				employee.setEmployeeid(Integer.valueOf(((String[]) params
						.get("id"))[0].toString()));

			} else {
				pw.write("Error");

				return;
			}
			this.service = ContextBuilder.getContext().getInstance(
					EmployeeServiceImpl.class);
			((EmployeeServiceImpl) service).deleteEmployee(employee);
			pw.write("Success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	}

}
